package cn.hqm.hqmtechnology.service.impl;

import cn.hqm.hqmtechnology.dao.dto.MenumDTO;
import cn.hqm.hqmtechnology.dao.mapper.MenumMapper;
import cn.hqm.hqmtechnology.dao.po.MemberRole;
import cn.hqm.hqmtechnology.dao.po.Menum;
import cn.hqm.hqmtechnology.dao.vo.MenumVO;
import cn.hqm.hqmtechnology.service.MemberRoleService;
import cn.hqm.hqmtechnology.service.MenumService;
import cn.hqm.hqmtechnology.utils.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MenumServiceImpl extends ServiceImpl<MenumMapper, Menum> implements MenumService {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private MemberRoleService memberRoleService;

    @Override
    public Object listMenum(String token,MenumDTO menumDTO) {
        Map<String, Object> forValue = (Map<String, Object>) redisUtil.getForValue(token);
        String memberId = (String) forValue.get("memberId");
        MemberRole role = memberRoleService.getOne(
                new LambdaQueryWrapper<MemberRole>()
                        .eq(MemberRole::getMemberId, memberId)
        );

        LambdaQueryWrapper<Menum> menumLambdaQueryWrapper = new LambdaQueryWrapper<>();
        Menum menum = new Menum();
        BeanUtils.copyProperties(menumDTO,menum);
        menumLambdaQueryWrapper.setEntity(menum);

        // 根据用户角色返回菜单列表
        Integer type = role.getType();
        switch (type) {
            case 1:
                // 普通用户
                menumLambdaQueryWrapper.eq(Menum::getIsRoot, 1);
                break;
        }
        List<Menum> list = this.list(menumLambdaQueryWrapper);
        List<MenumVO> menumVOs = new ArrayList<MenumVO>();
        list.forEach(item -> {
            if (item.getParentId().equals(0L)) {
                MenumVO menumVO = new MenumVO();
                BeanUtils.copyProperties(item, menumVO);
                menumVO.setId(item.getId().toString());
                menumVO.setParentId(item.getParentId().toString());
                menumVO.setSubList(new ArrayList<>());
                menumVOs.add(menumVO);
            } else {
                for (MenumVO menumVO : menumVOs) {
                    String id = menumVO.getId();
                    if (id.equals(item.getParentId().toString())) {
                        menumVO.getSubList().add(item);
                    }
                }
            }
        });
        return menumVOs;
    }

    @Override
    public Object addMenum(MenumDTO menumDTO) {
        Menum menum = new Menum();
        BeanUtils.copyProperties(menumDTO, menum);
        return this.save(menum);
    }

    @Override
    public Object delMenum(MenumDTO menumDTO, String token) {
        Map<String, Object> forValue = (Map<String, Object>) redisUtil.getForValue(token);
        Long memberId = (Long) forValue.get("memberId");
        MemberRole role = memberRoleService.getOne(
                new LambdaQueryWrapper<MemberRole>()
                        .eq(MemberRole::getMemberId, memberId)
        );
        if (!role.getType().equals(0)) {
            throw new RuntimeException("暂无权限!");
        }
        return this.remove(
                new LambdaQueryWrapper<Menum>()
                        .eq(Menum::getId, menumDTO.getId())
        );
    }
}